//
//  BrokenLine.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/6.
//  Copyright © 2016年 吴可高. All rights reserved.
//

#import "BrokenLine.h"
#import "BrokenPointValue.h"
#import "BrokenAnimation.h"
@interface BrokenLine()
{
    CAShapeLayer * _shapeLayer;
}
@end

@implementation BrokenLine

-(void)drawRect:(CGRect)rect
{
    [super  drawRect:rect];
    BrokenShapeLayer * brokenLineLayer = [BrokenShapeLayer  layer];
    brokenLineLayer.lineCap = kCALineCapRound;
    brokenLineLayer.lineJoin = kCALineJoinRound;
    brokenLineLayer.lineWidth = self.lineWidth;
    brokenLineLayer.strokeColor = self.lineColor.CGColor;
    
    CGMutablePathRef mutableRef = CGPathCreateMutable();
    
    for (int i = 0 ; i < self.items.count-1; i++)
    {
        BrokenPointValue * startValue = self.items[i];
        BrokenPointValue * endValue = self.items[i+1];
        CGPathMoveToPoint(mutableRef, &CGAffineTransformIdentity, [self getFrameX:startValue.pointX], [self getFrameY:startValue.pointY]);
        CGPathAddLineToPoint(mutableRef, &CGAffineTransformIdentity, [self getFrameX:endValue.pointX],[self getFrameY:endValue.pointY]);
    }
    brokenLineLayer.path = mutableRef;
    if (self.isAnimated)
    {
        [brokenLineLayer addAnimationKeyPath:@"strokeEnd" fromValue:@(0) toValue:@(1) duration:self.timeInterval delegate:self];
    }
    BrokenPointValue * startValue = self.items[0];
    
    [self drawIndicatorOrigin:CGPointMake([self getFrameX:startValue.pointX], [self getFrameY:startValue.pointY]) mutablePath:mutableRef];
    [self.layer addSublayer:brokenLineLayer];
}

-(void)drawIndicatorOrigin:(CGPoint)origin mutablePath:(CGMutablePathRef)mutablePathRef
{
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.backgroundColor = [UIColor redColor].CGColor;
    shapeLayer.cornerRadius = 10.f/2.f;
    shapeLayer.masksToBounds = YES;
    shapeLayer.frame = CGRectMake(0, 0, 10, 10);
    shapeLayer.position  = CGPointMake(origin.x, origin.y);
    [self.layer addSublayer:shapeLayer];
    _shapeLayer  = shapeLayer;
    
    CAKeyframeAnimation * keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.delegate = self;
    keyAnimation.duration = self.timeInterval;
    keyAnimation.autoreverses = NO;
    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.removedOnCompletion  = NO;

    keyAnimation.path = mutablePathRef;
    keyAnimation.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [shapeLayer addAnimation:keyAnimation forKey:@"position"];
}

-(void)animationDidStart:(CAAnimation *)anim
{
    [[UIApplication sharedApplication]beginIgnoringInteractionEvents];
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [[UIApplication sharedApplication]endIgnoringInteractionEvents];
}

-(UIColor *)lineColor
{
    return _lineColor ?  _lineColor:[UIColor redColor];
}

-(CGFloat)lineWidth
{
    return  _lineWidth  !=0 ? _lineWidth : 2.f;
}
@end
